package com.atguigu.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class RequestController {
    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request) {
        request.setAttribute("msg", "这是从request中获取的");
        request.setAttribute("code", 200);
        return "forward:/success";
    }

    @ResponseBody
    @GetMapping("/success")
    public Map success(HttpServletRequest request,
                       @RequestAttribute(value = "msg", required = false) String msg) {


        String msg1 = (String)request.getAttribute("msg");

        Map map = new HashMap();
        map.put("msg1", msg1);
        map.put("msg2", msg);

        Object mapMsg = request.getAttribute("mapMsg");
        Object modelMsg = request.getAttribute("modelMsg");
        Object requestMsg = request.getAttribute("requestMsg");
        map.put("mapMsg", mapMsg);
        map.put("modelMsg", modelMsg);
        map.put("requestMsg", requestMsg);

        return map;
    }

    @GetMapping("/params")
    public String testParam(Map<String, Object> map,
                            Model  model,
                            HttpServletRequest  request,
                            HttpServletResponse response){
        map.put("mapMsg", "这是从map中获取的");
        model.addAttribute("modelMsg", "这是从model中获取的");
        request.setAttribute("requestMsg", "这是从request中获取的");
        Cookie  cookie = new Cookie("c1", "v1");
        response.addCookie(cookie);
        return "forward:/success";
    }
};
